﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace FRM_Main
{
    public partial class FRM_NguoiDung : DevExpress.XtraEditors.XtraForm
    {
        Quan_Tri_He_ThongEntities5 context = new Quan_Tri_He_ThongEntities5();
        public FRM_NguoiDung()
        {
            InitializeComponent();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void FRM_NguoiDung_Load(object sender, EventArgs e)
        {
            dgvNguoDung.DataSource = context.SP_NGUOIDUNG_SELECT().ToList<NguoiDung>();
            LoadND();
           
        }
        private void LoadND()
        {
            var ND = from cat in context.NguoiDungs
                             select cat;
            dgvNguoDung.DataSource = ND.ToList<NguoiDung>();

        }
        private void xtabThemND_Click(object sender, EventArgs e)
        {

        }

        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            FRM_ThemNguoiDung f = new FRM_ThemNguoiDung();
            f.Show();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            FRM_PhanQuyen f = new FRM_PhanQuyen();
            f.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string TenDangNhap;
              TenDangNhap = dgvNguoDung.SelectedRows[0].Cells[8].Value.ToString();

                int? returnvalue = context.SP_NGUOIDUNG_DELETE(TenDangNhap.ToString()).SingleOrDefault();
                
               
                    if(returnvalue == 1)
                    {
                        MessageBox.Show("Xóa thành công");
                        LoadND();
                    }
                    else
                    {
                        MessageBox.Show("Không thể xóa người dùng");
                    }
            }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string TenDangNhap ;
            //int rowIndex = e.RowIndex;

            TenDangNhap = dgvNguoDung.SelectedRows[0].Cells[8].Value.ToString();
            FRM_SuaNguoiDung f = new FRM_SuaNguoiDung(TenDangNhap); 
            f.Show();
            ////string TenDangNhap;
            //  //TenDangNhap = dgvNguoDung.SelectedRows[0].Cells[0].Value.ToString();

            //    //int? returnvalue = context.SP_NGUOIDUNG_UPDATE
               
            //        if(returnvalue == 1)
            //        {
            //            MessageBox.Show("Xóa thành công");
            //            LoadND();
            //        }
            //        else
            //        {
            //            MessageBox.Show("Không thể xóa người dùng");
            //        }
            //}
        }

        private void dgvNguoDung_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string TenDangNhap = "";
            int rowIndex = e.RowIndex;

            TenDangNhap = dgvNguoDung.SelectedRows[0].Cells[8].Value.ToString();
            FRM_SuaNguoiDung f = new FRM_SuaNguoiDung(TenDangNhap);
            f.Show();
        }
        
    }
}